<?php
/**
 * 店面人员
 * User: ws
 * Date: 2018/8/30
 * Time: 16:25
 */

namespace app\admin\controller;
use app\admin\model\StoreUser as SuserModel;
use app\admin\model\CardOrder;
use think\db;
use think\Config;
class Suser  extends Init
{
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $where['name|phone'] = array('like','%'.trim($key).'%');
            $list = SuserModel::where($where)->order('store_user_id desc')->paginate(input('limit'))->toArray();
            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list');
    }


    function add(){
        if(request()->isPost()){
            $data = $this->request->post();

           $store_user_su = Db::table('je_store_user')->where('store_id',$data['store_id'])->order('store_user_id desc ')->value('store_user_id');
           if(empty($store_user_su)){
               $store_user_su = 0;
           }

            //生成编号，门店id开头，一共是十位数，前面四位是门店id，后面是id值，
            if(strlen($data['store_id'])==1){
                $card_pre = '000'.$data['store_id'];
            }elseif (strlen($data['store_id'])==2){
                $card_pre = '00'.$data['store_id'];
            }elseif (strlen($data['store_id'])==3){
                $card_pre = '0'.$data['store_id'];
            }elseif (strlen($data['store_id'])==4){
                $card_pre = $data['store_id'];
            }


            $wx = Db::name('wx_config')->where("config_id=1")->find();

            if($wx['date']<time()){
                $wx['access_token'] = $this->wx_acc();
            }
            $access = $wx['access_token'];

            $sn = $card_pre.str_pad($store_user_su+1, 5, '0', STR_PAD_LEFT);

            $data['sn'] = $sn;
            qrcode($access,$sn);

            $data['qrcode'] = "/uploads/qrcode/$sn.png";
            if (!SuserModel::create($data)) {
                return json(array('code'=>0,'msg'=>'添加失败'));
            }
            return json(array('code'=>200,'msg'=>'添加成功'));
        }

        $store = Db::name('store')->field('store_id,name')->select();
        return view('add',['store'=>$store]);
    }


    function edit(){
        if(request()->isPost()){
            $data = $this->request->post();

            $wx = Db::name('wx_config')->where("config_id=1")->find();

            if($wx['date']<time()){
                $wx['access_token'] = $this->wx_acc();
            }
            $access = $wx['access_token'];
            unlink('./uploads/qrcode/'.$data['sn'].'.png');

            qrcode($access,$data['sn']);

            $data['qrcode'] = "/uploads/qrcode/".$data['sn'].".png";
            if (!SuserModel::update($data)) {
                return json(array('code'=>0,'msg'=>'修改失败'));
            }
            return json(array('code'=>200,'msg'=>'修改成功'));
        }

        $list = SuserModel::where('store_user_id', input('id'))->find()->toArray();
        $store = Db::name('store')->field('store_id,name')->select();
        return view('edit',['list'=>$list,'store'=>$store]);
    }



    function del(){
        $id = input('param.sn');
        $data = CardOrder::where('store_user_sn',"$id")->find();
        $suser = SuserModel::where('sn',"$id")->find()->toArray();
        if($data){
            return json(array('code'=>0,'msg'=>'删除失败，已销售不能被删除'));
        }
        if (!SuserModel::destroy(['sn' => "$id"])) {
            return json(array('code'=>0,'msg'=>'删除失败'));
        }
        unlink('.'.$suser['qrcode']);
        return json(array('code'=>200,'msg'=>'删除成功'));
    }



    //获取Access_token
    function wx_acc(){
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".Config::get('wechat.appid')."&secret=".Config::get('wechat.appsecret')."";
        $json = httpRequest($url);
        $json = json_decode($json,true);
        $data['access_token'] = $json['access_token'];
        $data['expires_in'] = $json['expires_in'];
        $data['date'] = time()+7200;
        Db::table('je_wx_config')->where('config_id', 1)->update($data);
        return $data['access_token'];
    }
}